#include "client.h"
#include "ui_client.h"
#include <QDebug>
#include <QMouseEvent>
#include <playcenterpanelbase.h>

Client::Client(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Client),need_tributer(0)
{
    ui->setupUi(this);
    init();
    changeState(play_state::PREPARE);
}

//初始化牌
void Client::setPoker(int num[]){
    int k = 0;
    //初始化mark数组，并设置好不是自己的牌
    Poker * p = nullptr;
    for(int i = 0;i < 54;++i){
        //如果是自己牌
        if(i == num[k]){
            mark[i] = 1;
            ++k;
        }else{
            p = new Poker(this,54);
            p->setCanClick(false);
            p->hide();
            other_poker.push_back(p);
            all_poker.push_back(p);
            mark[i] = 0;
        }

    }
    //设置自己的牌数组
    for(int i = 0;i < 13;++i){
        for(int j = 0;j < 4;++j){
            if(mark[i + j*13] == 1){
                p = new Poker(this,i + j*13);
                p->hide();
                //p->raise();
                own_poker.push_back(p);
                all_poker.push_back(p);
            }
        }
    }
    //设置大小王
    if(mark[52] == 1){
        p = new Poker(this,52);
        p->hide();
        //p->raise();
        own_poker.push_back(p);
        all_poker.push_back(p);
    }
    if(mark[53] == 1){
        p = new Poker(this,53);
        p->hide();
        //p->raise();
        own_poker.push_back(p);
        all_poker.push_back(p);
    }

}

//初始化
void Client::init(){
    this->resize(QSize(1540,920));
    this->setFixedSize(this->width(), this->height());
    this->setWindowFlags(this->windowFlags()& ~Qt::WindowMaximizeButtonHint);
    int num[] = {0,1,6,13,14,16,18,19,21,26,27,32,34,35,40,45,52,53};
    //初始化牌
    setPoker(num);
    //设置其他牌数量
    last_number = 18;
    next_number = 18;

}

//bool Client::eventFilter(QObject* watched,QEvent * event){
//    if(watched == poker ){
//        qDebug() << "Client:"<< event->type();
//    }
//    return false;
//}

//void Client::mousePressEvent(QMouseEvent * event){
//    qDebug() << event->type();
//}
//CenterPanelBase *find_room;//寻找房间
//CenterPanelBase *deal_card;//发牌
//CenterPanelBase *prepare;//准备阶段
//CenterPanelBase *tribute;//准备阶段
//CenterPanelBase *play;//运行阶段
//CenterPanelBase *finish;//结算阶段

void Client::changeState(play_state state_end){

    if(state_end == play_state::PREPARE){//收到准备状态结束选项
        qDebug() << "准备状态结束";
        //设置deal_card
        deal_card = new DealCardCenterPanel(this,mark,own_poker,other_poker,&last_number,&next_number);
        connect(deal_card,&CenterPanelBase::state_end,this,&Client::changeState);
    }else if(state_end == play_state::DEAL_CARD){//收到发牌结束选项
        delete deal_card;//删除发牌阶段资源
        qDebug() << "发牌结束";
        tribute = new TributeCenterPanel(this,mark,own_poker,other_poker,&last_number,&next_number);
        connect(tribute,&CenterPanelBase::state_end,this,&Client::changeState);
        static_cast<TributeCenterPanel *>(tribute)->run_tribute();
        //changeState(play_state::TRIBUTE);
    }else if(state_end == play_state::TRIBUTE){//收到发牌结束选项
        delete tribute;//删除进贡阶段资源
        qDebug() << "进贡结束";
        play = new PlayCenterPanelBase(this,mark,own_poker,other_poker,&last_number,&next_number);//开启运行阶段

        player_type player_win = static_cast<PlayCenterPanelBase *>(play)->run_game();
        if(player_win == player_type::LAST) qDebug() << "上家win";
        if(player_win == player_type::MY) qDebug() << "自己win";
        if(player_win == player_type::NEXT) qDebug() << "下家win";
        changeState(play_state::PLAY);

    }else if(state_end == play_state::PLAY){//收到运行结束选项
        delete play;//释放运行阶段资源
        qDebug() << "运行结束";
        finish = nullptr;//开启结算阶段
        //connect(deal_card,&CenterPanelBase::state_end,this,&Client::changeState);
        changeState(play_state::FINISH);
    }else if(state_end == play_state::FINISH){//收到结算完成选项

    }
}

bool Client::event(QEvent * event){
//    qDebug() << event->type();
    if(event->type() == QEvent::Resize){
        qDebug() << this->size();
    }
    return QWidget::event(event);
}


Client::~Client()
{
    delete ui;
}

